﻿#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QDataStream>
#include <QSerialPort>
#include <QSerialPortInfo>

class SerialPort
{
public:
    SerialPort();

    //将1-9 a-f字符转换为对应的整数
    char ConvertHexChar(char ch)
    {
        if((ch >= '0') && (ch <= '9'))
            return ch-0x30;
        else if((ch >= 'A') && (ch <= 'F'))
            return ch-'A'+10;
        else if((ch >= 'a') && (ch <= 'f'))
            return ch-'a'+10;
        else
            return -1;
    }

    //将字符型进制转化为16进制的字节数组
    QByteArray QString2Hex(QString str, bool &flag)
    {
        QByteArray senddata;
        int hexdata,lowhexdata;
        int hexdatalen = 0;
        int len = str.length();
        char lstr,hstr;

        senddata.resize(len/2);

        for(int i=0;i<len;i++)
        {
            hstr = str[i].toLatin1();   //字符型
            if(hstr == ' ')
                continue;

            i++;

            lstr = str[i].toLatin1();
            if(lstr == ' ' || i >= len) //保证单字符或最后一个单字符的情况下发送正确
            {
                hexdata = 0;
                lowhexdata = ConvertHexChar(hstr);
            }
            else
            {
                hexdata = ConvertHexChar(hstr);
                lowhexdata = ConvertHexChar(lstr);
            }

            if((hexdata == -1) || (lowhexdata == -1))   //输入不合法
            {
                flag = false;
                senddata.resize(hexdatalen);
                return senddata;
            }
            else
                hexdata = hexdata*16 + lowhexdata;

            senddata[hexdatalen] = static_cast<char>(hexdata);
            hexdatalen++;

        }
        senddata.resize(hexdatalen);
        return senddata;
    }

    //将接收的一串QByteArray类型的16进制转化为对应的字符串16进制
    QString ShowHex(QByteArray str)
    {
        QDataStream out(&str, QIODevice::ReadWrite);    //将str的数据 读到out里面去
        QString buf;
        while (!out.atEnd()) {
            qint8 outChar = 0;
            out >> outChar;
            QString str = QString("%1").arg(outChar&0xFF, 2, 16, QLatin1Char('0')).toUpper() + QString(" ");    //2字符宽度

            buf += str;
        }

        return buf;
    }

    QSerialPort::BaudRate baudRate(int index)
    {
        QSerialPort::BaudRate baudRate = QSerialPort::UnknownBaud;
        Q_ASSERT(index >= 0 && index < 8);

        switch (index) {
            case 0:
                baudRate = QSerialPort::Baud1200;
                break;
            case 1:
                baudRate = QSerialPort::Baud2400;
                break;
            case 2:
                baudRate = QSerialPort::Baud4800;
                break;
            case 3:
                baudRate = QSerialPort::Baud9600;
                break;
            case 4:
                baudRate = QSerialPort::Baud19200;
                break;
            case 5:
                baudRate = QSerialPort::Baud38400;
                break;
            case 6:
                baudRate = QSerialPort::Baud57600;
                break;
            case 7:
                baudRate = QSerialPort::Baud115200;
                break;
            default:
                baudRate = QSerialPort::UnknownBaud;
                break;
        }

        return baudRate;
    }

    QSerialPort::DataBits dataBits(int index)
    {
        QSerialPort::DataBits dataBits = QSerialPort::UnknownDataBits;
        Q_ASSERT(index >= 0 && index < 4);

        switch (index) {
        case 0:
            dataBits = QSerialPort::Data5;
            break;
        case 1:
            dataBits = QSerialPort::Data6;
            break;
        case 2:
            dataBits = QSerialPort::Data7;
            break;
        case 3:
            dataBits = QSerialPort::Data8;
            break;
        default:
            dataBits= QSerialPort::UnknownDataBits;
            break;
        }

        return dataBits;
    }

    QSerialPort::Parity parity(int index)
    {
        QSerialPort::Parity parity = QSerialPort::UnknownParity;
        Q_ASSERT(index >= 0 && index < 5);

        switch(index){
        case 0:
            parity = QSerialPort::NoParity;     //无校验
            break;
        case 1:
            parity = QSerialPort::EvenParity;   //偶检验
            break;
        case 2:
            parity = QSerialPort::OddParity;    //奇校验
            break;
        case 3:
            parity = QSerialPort::SpaceParity;  //空格校验
            break;
        case 4:
            parity = QSerialPort::MarkParity;   //Mark
            break;
        default:
            parity = QSerialPort::UnknownParity;
            break;
        }

        return parity;
    }

    QSerialPort::StopBits stopBits(int index)
    {
        QSerialPort::StopBits stopBits = QSerialPort::UnknownStopBits;
        Q_ASSERT(index >= 0 && index < 3);

        switch (index) {
        case 0:
            stopBits = QSerialPort::OneStop;
            break;
        case 1:
            stopBits = QSerialPort::OneAndHalfStop;
            break;
        case 2:
            stopBits = QSerialPort::TwoStop;
            break;
        default:
            stopBits = QSerialPort::UnknownStopBits;
            break;
        }

        return stopBits;
    }

    //BCD码转换
    unsigned char BCD2Decimal(unsigned char bcd)
    {
        unsigned char Decimal = bcd >> 4;
        return (Decimal*10) + (bcd &= 0x0f);
    }
};

#endif // SERIALPORT_H
